<?php
namespace Admin\Service;
//商品类
use Admin\AppConst\WebConst;
use Admin\Model\RoleModel;

class RoleService extends BaseService
{

    /**
     * @var RoleService
     */
    private static $instance;

    /**
     * Get the instance of RoleService.
     * @return RoleService
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new RoleService();
        }
        return self::$instance;
    }


    /**
     * @function    getList     //菜单数据
     * @author      zhangle
     * @date        2018-07-26
     * @return      array
     */
    public function getList(){
        $result = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;

        //获取RoleId
        $list   = RoleModel::get()->getList();

        if(!empty($list)){
            $menu   = MenuService::get()->getListNotTrees();
            $menu   = $this->toMap($menu, 'id', 'name');

            foreach ($list as $value){
                if($value['menu_ids']){
                    $menuIdList = explode(',', $value['menu_ids']);
                    $menuNameList   = array();
                    foreach ($menuIdList as $val) {
                        $menuNameList[] = $menu[$val];
                    }
                    $value['menuNameList']  = implode(',', $menuNameList);
                }
                $result[] = $value;
            }
        }
        return $result;
    }

    /**
     * @function    getListNotMenu     //角色数据
     * @author      zhangle
     * @date        2018-07-26
     * @return      array
     */
    public function getListTitle(){
        $list   = RoleModel::get()->getList();
        $list   = $this->toMap($list, 'id', 'title');

        return $list;
    }

    /**
     * @function    createRole         创建
     * @param       $title      string  角色名称
     * @param       $content    string  说明
     * @param       $menuIdList string  权限id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function createRole($title, $content, $menuIdList){
        return RoleModel::get()->createRole($title, $content, $menuIdList);
    }

    /**
     * @function    editRole    更新
     * @param       $id         int     id
     * @param       $title      string  角色名称
     * @param       $content    string  说明
     * @param       $menuIdList string  权限id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function editRole($id, $title, $content, $menuIdList){
        return RoleModel::get()->editRole($id, $title, $content, $menuIdList);
    }

    /**
     * @function    getRoleById         查询单个
     * @param       $id         int     id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function getRoleById($id){
        return RoleModel::get()->getRoleById($id);
    }


    /**
     * @function    deleteRole  查询单个
     * @param       $id         int     id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function deleteRole($id){
        return RoleModel::get()->deleteRole($id);
    }

    /**
     * @function    checkRole   验证权限
     * @param       $menuId     int     菜单id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function checkRole($menuId){
        $menuList  = session(WebConst::USER_SESSION_KEY)['menuList'];
        if(!in_array($menuId, $menuList)){
            return false;
        }
        return true;
    }

}